package com.bsoft.gol.hcs.specialist.service.impl;

import com.alibaba.fastjson.JSON;
import com.bsoft.gol.hcs.his.bean.response.DeptLeaderListRepHisBean;
import com.bsoft.gol.hcs.his.service.SendMsgService;
import com.bsoft.gol.hcs.specialist.bean.qo.*;
import com.bsoft.gol.hcs.specialist.bean.vo.*;
import com.bsoft.gol.hcs.specialist.dao.BaseComplaintAttachDAO;
import com.bsoft.gol.hcs.specialist.dao.BaseComplaintDAO;
import com.bsoft.gol.hcs.specialist.dao.BaseComplaintRectDAO;
import com.bsoft.gol.hcs.specialist.dao.BaseComplaintTraceDAO;
import com.bsoft.gol.hcs.specialist.service.BaseComplaintService;
import com.bsoft.gol.hcs.specialist.service.IPatientSearchRuleExecuteService;
import com.bsoft.gol.hcs.utils.DateUtils;
import ctd.account.UserRoleToken;
import ctd.controller.exception.ControllerException;
import ctd.net.rpc.Client;
import ctd.spring.boot.annotation.RpcAccess;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.BeanUtils;
import ctd.util.annotation.RpcService;
import hcn.base.notification.event.NotificationEvent;
import hcn.bean.PushMessageBean;
import hcn.bean.QueryAuthorityOrgListQo;
import hcn.bean.qo.FindComplaintTeamListQo;
import hcn.bean.vo.QueryAuthorityOrgListVo;
import hcn.enums.MsgTemplateCode;
import hcn.service.base.qo.DoctorListByMemQo;
import hcn.service.base.vo.DeptTreeVo;
import hcn.service.base.vo.DeptVo;
import hcn.specialist.BaseComplaint;
import hcn.specialist.BaseComplaintAttach;
import hcn.specialist.BaseComplaintRect;
import hcn.specialist.BaseComplaintTrace;
import hcn.util.CommonUtils;
import lombok.SneakyThrows;
import org.atag.dao.model.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import pcn.enums.RoleIdEnum;
import service.rpc.DoctorInfoServiceIntf;
import service.rpc.IDocTeamMemberRpcService;
import service.rpc.IDoctorInfoRpcService;
import service.rpc.IOrganizationRpcService;

import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * @Author zsf
 * @Date 2023/10/19 0019 9:39
 * @PackageName:com.bsoft.gol.hcs.specialist.service.impl
 * @ClassName: BaseComplaintServiceImpl
 * @Description: TODO
 * @Version 4.0.02
 */
@SsdevService(id = "baseComplaint", name = "baseComplaintService")
public class BaseComplaintServiceImpl implements BaseComplaintService {
    private final static Logger log = LoggerFactory.getLogger(BaseComplaintServiceImpl.class);

    @Autowired
    private BaseComplaintDAO baseComplaintDAO;
    @Autowired
    private BaseComplaintAttachDAO baseComplaintAttachDAO;
    @Autowired
    private BaseComplaintRectDAO baseComplaintRectDAO;
    @Autowired
    private BaseComplaintTraceDAO baseComplaintTraceDAO;
    @Autowired
    private DoctorInfoServiceIntf hcnDoctorInfoService;
    @Autowired
    private IOrganizationRpcService hcnOrganizationRpcService;
    @Autowired
    private SendMsgService sendMsgService;
    @Autowired
    private IDoctorInfoRpcService iDoctorInfoRpcService;
    @Autowired
    private IDocTeamMemberRpcService docTeamMemberRpcService;
    @Autowired
    private IPatientSearchRuleExecuteService patientSearchRuleExecuteService;

    /**
     * @param qo
     * @return void
     * @description: 添加投诉
     * @author: zsf
     * @date: 2023/10/19 0019 10:12
     */
    @RpcService
    @Override
    public BaseComplaint addOrUpdate(AddComplaintQo qo) {
        log.info("BaseComplaintServiceImpl-addOrUpdate入参:{}", JSON.toJSONString(qo));
        BaseComplaint vo = new BaseComplaint();
        //这里看是否存在投诉主键如果存在就是更新,否则是新增
        BaseComplaint baseComplaint = new BaseComplaint();
        BeanUtils.copy(qo, baseComplaint);
        if (CommonUtils.isNotEmpty(baseComplaint.getComplaintId())) {
            //更新反馈附件
            if (CommonUtils.isNotEmpty(qo.getFileList())) {
                ArrayList<Integer> attachIdList = new ArrayList<>();
                for (BaseComplaintAttach baseComplaintAttach : qo.getFileList()) {
                    if (CommonUtils.isNotEmpty(baseComplaintAttach.getAttachId())) {
                        baseComplaintAttachDAO.update(baseComplaintAttach);
                        attachIdList.add(baseComplaintAttach.getAttachId());
                    } else {
                        baseComplaintAttach.setRelevancyId(baseComplaint.getComplaintId());
                        baseComplaintAttach.setCreateTime(new Date());
                        BaseComplaintAttach save = baseComplaintAttachDAO.save(baseComplaintAttach);
                        attachIdList.add(save.getAttachId());
                    }
                }
                baseComplaintAttachDAO.removeByAttachIdList(baseComplaint.getComplaintId(), attachIdList);
            }
            //更新整改内容
            if (CommonUtils.isNotEmpty(qo.getRectList())) {
                ArrayList<String> rectIdList = new ArrayList<>();
                for (AddComplaintRectBean addComplaintRectBean : qo.getRectList()) {
                    BaseComplaintRect baseComplaintRect = new BaseComplaintRect();
                    BeanUtils.copy(addComplaintRectBean, baseComplaintRect);
                    //整改id非空就更新,空的就新增
                    if (CommonUtils.isNotEmpty(baseComplaintRect.getRectId())) {
                        baseComplaintRectDAO.update(baseComplaintRect);
                        rectIdList.add(baseComplaintRect.getRectId());
                        //修改整改里面的附件
                        if (CommonUtils.isNotEmpty(addComplaintRectBean.getFileList())) {
                            ArrayList<Integer> attachIdList = new ArrayList<>();
                            for (BaseComplaintAttach baseComplaintAttach : addComplaintRectBean.getFileList()) {
                                if (CommonUtils.isNotEmpty(baseComplaintAttach.getAttachId())) {
                                    baseComplaintAttachDAO.update(baseComplaintAttach);
                                    attachIdList.add(baseComplaintAttach.getAttachId());
                                } else {
                                    baseComplaintAttach.setCreateTime(new Date());
                                    BaseComplaintAttach save = baseComplaintAttachDAO.save(baseComplaintAttach);
                                    attachIdList.add(save.getAttachId());
                                }
                            }
                            baseComplaintAttachDAO.removeByAttachIdList(baseComplaintRect.getRectId(), attachIdList);
                        }
                    } else {
                        baseComplaintRect.setRectId(UUID.randomUUID().toString().replaceAll("-", ""));
                        baseComplaintRect.setCreateTime(new Date());
                        baseComplaintRect.setComplaintId(baseComplaint.getComplaintId());
                        baseComplaintRectDAO.save(baseComplaintRect);
                        rectIdList.add(baseComplaintRect.getRectId());
                        //修改整改里面的附件
                        if (CommonUtils.isNotEmpty(addComplaintRectBean.getFileList())) {
                            ArrayList<Integer> attachIdList = new ArrayList<>();
                            for (BaseComplaintAttach baseComplaintAttach : addComplaintRectBean.getFileList()) {
                                baseComplaintAttach.setRelevancyId(baseComplaintRect.getRectId());
                                baseComplaintAttach.setCreateTime(new Date());
                                BaseComplaintAttach save = baseComplaintAttachDAO.save(baseComplaintAttach);
                                attachIdList.add(save.getAttachId());
                            }
                            baseComplaintAttachDAO.removeByAttachIdList(baseComplaintRect.getRectId(), attachIdList);
                        }
                    }
                }
                //当是医生进行整改是不要删除没有传的科室,因为这里医生1只能拿到自己当前自己拥有的执业点的科室
                if (0 == qo.getRectIsFlag()) {
                    baseComplaintRectDAO.removeByRectIdList(qo.getComplaintId(), rectIdList);
                } else {
                    //需要判断是否所有的科室都整改完成了如果没有则变成部分整改中,全部整改科室整改完成则需要变成已完成整改到整改审核
                    List<BaseComplaintRect> complaintRectStatus = baseComplaintRectDAO.findComplaintRectStatus(qo.getComplaintId());
                    if (CommonUtils.isNotEmpty(complaintRectStatus)) {
                        baseComplaint.setStatus(4);
                    } else {
                        baseComplaint.setStatus(6);
                    }
                }
            }
            vo = baseComplaintDAO.update(baseComplaint);
            //被驳回的整改要把科室整改状态变成未整改
            if (qo.getStatus() == 7 && qo.getRectIsFlag() == 0) {
                baseComplaintRectDAO.updateStatusByComplaintId(vo.getComplaintId(), (byte) 0);
            }
            //处理审核通过之后要给患者发送消息通知他已经完成了在处理了->处理审核的时候只有通过未通过没有-整改打回也要在这里修改,并且不是整改处理的时候才会传rectIsFlag=0
            if ((qo.getStatus() == 5 || qo.getStatus() == 8) && qo.getRectIsFlag() == 0) {
                // 极光-->通知居民
                PushMessageBean pushMessageBean = new PushMessageBean();
                pushMessageBean.setNotificateType(String.valueOf(NotificationEvent.NotificateType.feedback));
                pushMessageBean.setTemplateCode(MsgTemplateCode.FEEDBACK.getCode());
                pushMessageBean.setRoleId(RoleIdEnum.PATIENT.title());
                pushMessageBean.setUserId(vo.getOpenId());
                pushMessageBean.setExtras(null);
                pushMessageBean.setMsgVariableMap(null);
                try {
                    sendMsgService.productionMessage(pushMessageBean);
                } catch (Exception e) {
                    log.error("居民端投诉处理审核完成通知居民失败:{}", JSON.toJSONString(e));
                }
                //极光通知医生去整改
                /*List<BaseComplaintRect> complaintRectStatus = baseComplaintRectDAO.findComplaintRect(qo.getComplaintId());
                for (BaseComplaintRect rectStatus : complaintRectStatus) {
                    if (CommonUtils.isNotEmpty(rectStatus.getDoctorIds())) {
                        String[] doctorIds = rectStatus.getDoctorIds().split(",");
                        for (String doctorId : doctorIds) {
                            String doctorUserId = null;
                            try {
                                doctorUserId = iDoctorInfoRpcService.getDoctorUserId(doctorId);
                            } catch (Throwable e) {
                                log.error("投诉审核通过通知整改医生,查询医生userId:{}", doctorUserId);
                            }
                            PushMessageBean pushMessageBeanDoctor = new PushMessageBean();
                            pushMessageBeanDoctor.setNotificateType(String.valueOf(NotificationEvent.NotificateType.doctFeedback));
                            pushMessageBeanDoctor.setTemplateCode(MsgTemplateCode.DOCTFEEDBACK.getCode());
                            pushMessageBeanDoctor.setRoleId(RoleIdEnum.DOCTOR.title());
                            pushMessageBeanDoctor.setUserId(doctorUserId);
                            pushMessageBeanDoctor.setExtras(null);
                            pushMessageBeanDoctor.setMsgVariableMap(null);
                            try {
                                sendMsgService.productionMessage(pushMessageBeanDoctor);
                            } catch (Exception e) {
                                log.error("居民端投诉处理审核完成通知居民失败:{}", JSON.toJSONString(e));
                            }
                        }
                    }
                }*/
                // 发送钉钉文本消息
                for (AddComplaintRectBean addComplaintRectBean : qo.getRectList()) {
                    if(!StringUtils.isEmpty(addComplaintRectBean.getDoctorPhone())){
                        String[] phoneNos = addComplaintRectBean.getDoctorPhone().split(",");
                        for (String phoneNo : phoneNos) {
                            try {
                                Client.rpcInvoke("hcn.dingSendMessageService", "sendTextMsgByMobile", "你有一条投诉待整改请去查看", phoneNo);
                            } catch (Exception e) {
                                log.error("发送钉钉消息失败：", e);
                            }
                        }
                    }
                }
            }
            //通知医生被整改被打回,重新整改
            if (qo.getStatus() == 7 && qo.getRectIsFlag() == 0) {
                /*List<BaseComplaintRect> complaintRectStatus = baseComplaintRectDAO.findComplaintRect(qo.getComplaintId());
                for (BaseComplaintRect rectStatus : complaintRectStatus) {
                    if (CommonUtils.isNotEmpty(rectStatus.getDoctorIds())) {
                        String[] doctorIds = rectStatus.getDoctorIds().split(",");
                        for (String doctorId : doctorIds) {
                            String doctorUserId = null;
                            try {
                                doctorUserId = iDoctorInfoRpcService.getDoctorUserId(doctorId);
                            } catch (Throwable e) {
                                log.error("投诉审核通过通知整改医生,查询医生userId:{}", doctorUserId);
                            }
                            PushMessageBean pushMessageBeanDoctor = new PushMessageBean();
                            pushMessageBeanDoctor.setNotificateType(String.valueOf(NotificationEvent.NotificateType.sugg_tobe_commit));
                            pushMessageBeanDoctor.setTemplateCode(MsgTemplateCode.SUGG_TOBE_COMMIT.getCode());
                            pushMessageBeanDoctor.setRoleId(RoleIdEnum.DOCTOR.title());
                            pushMessageBeanDoctor.setUserId(doctorUserId);
                            pushMessageBeanDoctor.setExtras(null);
                            pushMessageBeanDoctor.setMsgVariableMap(null);
                            try {
                                sendMsgService.productionMessage(pushMessageBeanDoctor);
                            } catch (Exception e) {
                                log.error("居民端投诉处理审核驳回通知居民失败:{}", JSON.toJSONString(e));
                            }
                        }
                    }
                }*/
                // 发送钉钉文本消息
                for (AddComplaintRectBean addComplaintRectBean : qo.getRectList()) {
                    if(!StringUtils.isEmpty(addComplaintRectBean.getDoctorPhone())) {
                        String[] phoneNos = addComplaintRectBean.getDoctorPhone().split(",");
                        for (String phoneNo : phoneNos) {
                            try {
                                Client.rpcInvoke("hcn.dingSendMessageService", "sendTextMsgByMobile", "你有一条投诉整改被打回请去查看", phoneNo);
                            } catch (Exception e) {
                                log.error("发送钉钉消息失败：", e);
                            }
                        }
                    }
                }
            }
        } else {
            baseComplaint.setComplaintId(UUID.randomUUID().toString().replaceAll("-", ""));
            baseComplaint.setCreateTime(new Date());
            vo = baseComplaintDAO.save(baseComplaint);
            if (CommonUtils.isNotEmpty(qo.getFileList())) {
                for (BaseComplaintAttach baseComplaintAttach : qo.getFileList()) {
                    baseComplaintAttach.setRelevancyId(baseComplaint.getComplaintId());
                    baseComplaintAttach.setCreateTime(new Date());
                    baseComplaintAttachDAO.save(baseComplaintAttach);
                }
            }
            if (CommonUtils.isNotEmpty(qo.getRectList())) {
                for (AddComplaintRectBean addComplaintRectBean : qo.getRectList()) {
                    BaseComplaintRect baseComplaintRect = new BaseComplaintRect();
                    BeanUtils.copy(addComplaintRectBean, baseComplaintRect);
                    baseComplaintRect.setComplaintId(baseComplaint.getComplaintId());
                    baseComplaintRect.setRectId(UUID.randomUUID().toString().replaceAll("-", ""));
                    baseComplaintRect.setCreateTime(new Date());
                    baseComplaintRectDAO.save(baseComplaintRect);
                    if (CommonUtils.isNotEmpty(addComplaintRectBean.getFileList())) {
                        for (BaseComplaintAttach baseComplaintAttach : addComplaintRectBean.getFileList()) {
                            baseComplaintAttach.setRelevancyId(baseComplaintRect.getRectId());
                            baseComplaintAttachDAO.save(baseComplaintAttach);
                        }
                    }
                }
            }
        }
        return vo;
    }

    /**
     * @param qo
     * @return void
     * @description: 更新app状态-->医生端整改更新,不可用于pc端更新
     * @author: zsf
     * @date: 2023/10/31 0031 16:16
     */
    @RpcService
    @Override
    public void updateAppComplaint(UpdateAppComplaintQo qo) {
        BaseComplaint baseComplaint = baseComplaintDAO.get(qo.getComplaintId());
        if (baseComplaint.getStatus().equals(qo.getStatus())) {
            BaseComplaintTrace baseComplaintTrace = new BaseComplaintTrace();
            baseComplaintTrace.setOperatTime(new Date());
            baseComplaintTrace.setCreateTime(new Date());
            baseComplaintTrace.setComplaintId(qo.getComplaintId());
            baseComplaintTrace.setOperatId(UserRoleToken.getCurrent().getLoginName());
            baseComplaintTrace.setOperatName(UserRoleToken.getCurrent().getUserName());
            baseComplaintTraceDAO.save(baseComplaintTrace);
        }
        //修改整改里面的附件
        if (CommonUtils.isNotEmpty(qo.getFileList())) {
            ArrayList<Integer> attachIdList = new ArrayList<>();
            for (BaseComplaintAttach baseComplaintAttach : qo.getFileList()) {
                if (CommonUtils.isNotEmpty(baseComplaintAttach.getAttachId())) {
                    baseComplaintAttachDAO.update(baseComplaintAttach);
                    attachIdList.add(baseComplaintAttach.getAttachId());
                } else {
                    baseComplaintAttach.setCreateTime(new Date());
                    BaseComplaintAttach save = baseComplaintAttachDAO.save(baseComplaintAttach);
                    attachIdList.add(save.getAttachId());
                }
            }
            baseComplaintAttachDAO.removeByAttachIdList(qo.getRectId(), attachIdList);
        }
        baseComplaintRectDAO.updateComplaintRect(qo.getRectId(), qo.getAccurReason(), qo.getRectContent(), qo.getSystemProcess(), qo.getRectStatus());
        //需要判断是否所有的科室都整改完成了如果没有则变成部分整改中,全部整改科室整改完成则需要变成已完成整改到整改审核
        List<BaseComplaintRect> complaintRectStatus = baseComplaintRectDAO.findComplaintRectStatus(qo.getComplaintId());
        //当状态是完成整改时,要判断是否所有的科室都整改完成了,否则显示部分整改
        if (qo.getStatus() == 6) {
            if (CommonUtils.isNotEmpty(complaintRectStatus)) {
                qo.setStatus(4);
            } else {
                qo.setStatus(6);
            }
        }
        baseComplaintDAO.updateStatus(qo.getComplaintId(), qo.getStatus());
    }

    /**
     * @param qo
     * @return void
     * @description: 居民端app新增投诉
     * @author: zsf
     * @date: 2023/10/23 0023 15:02
     */
    @RpcAccess(auth = false)
    @RpcService
    @Override
    public void addOrUpdateNoAuth(AddComplaintQo qo) {
        log.info("excute method begin addOrUpdateNoAuth,param:"+qo);
        BaseComplaint baseComplaint = new BaseComplaint();
        BeanUtils.copy(qo, baseComplaint);
        baseComplaint.setComplaintId(UUID.randomUUID().toString().replaceAll("-", ""));
        baseComplaint.setCreateTime(new Date());
        baseComplaint.setStatus(0);
        //投诉时间
        baseComplaint.setComplaintTime(new Date());
        baseComplaintDAO.save(baseComplaint);
        if (CommonUtils.isNotEmpty(qo.getFileList())) {
            for (BaseComplaintAttach baseComplaintAttach : qo.getFileList()) {
                baseComplaintAttach.setRelevancyId(baseComplaint.getComplaintId());
                baseComplaintAttach.setCreateTime(new Date());
                baseComplaintAttachDAO.save(baseComplaintAttach);
            }
        }
    }

    /**
     * @param qo
     * @return com.bsoft.gol.hcs.specialist.bean.vo.FindComplaintListNoAuthVo
     * @description: 查询投诉列表
     * @author: zsf
     * @date: 2023/10/23 0023 15:32
     */
    @RpcAccess(auth = false)
    @RpcService
    @Override
    public PageInfo<FindComplaintListNoAuthVo> findComplaintListNoAuth(FindComplaintListNoAuthQo qo) {
        PageInfo<FindComplaintListNoAuthVo> vos = new PageInfo<>();
        List<FindComplaintListNoAuthVo> vo = new ArrayList<>();
        int start = (qo.getPageNo() - 1) * qo.getPageSize();
        List<BaseComplaint> list = baseComplaintDAO.findComplaintList(qo.getOpenId(), start, qo.getPageSize());
        Long total = baseComplaintDAO.getComplaintListTotal(qo.getOpenId());
        if (CommonUtils.isNotEmpty(list)) {
            for (BaseComplaint baseComplaint : list) {
                FindComplaintListNoAuthVo bean = new FindComplaintListNoAuthVo();
                BeanUtils.copy(baseComplaint, bean);
                List<BaseComplaintAttach> fileList = baseComplaintAttachDAO.findComplaintAttach(baseComplaint.getComplaintId());
                bean.setFileList(fileList);
                vo.add(bean);
            }
            vos.setList(vo);
            vos.setTotal(total);
            return vos;
        }
        return null;
    }

    /**
     * @param qo
     * @return com.bsoft.gol.hcs.specialist.bean.vo.GetComplaintListNoAuthVo
     * @description: 居民端查询投诉详情
     * @author: zsf
     * @date: 2023/10/23 0023 17:05
     */
    @RpcAccess(auth = false)
    @RpcService
    @Override
    public GetComplaintListNoAuthVo getComplaintListNoAuth(GetComplaintListNoAuthQo qo) {
        GetComplaintListNoAuthVo vo = new GetComplaintListNoAuthVo();
        BaseComplaint baseComplaint = baseComplaintDAO.get(qo.getComplaintId());
        BeanUtils.copy(baseComplaint, vo);
        List<BaseComplaintAttach> attachList = baseComplaintAttachDAO.findComplaintAttach(qo.getComplaintId());
        vo.setFileList(attachList);
        return vo;
    }

    /**
     * @param qo
     * @return com.bsoft.gol.hcs.specialist.bean.vo.GetComplaintDetailVo
     * @description: 查询投诉列表详情
     * @author: zsf
     * @date: 2023/10/19 0019 13:43
     */
    @RpcService
    @Override
    public GetComplaintDetailVo getComplaintDetail(GetComplaintDetailQo qo) {
        log.info("BaseComplaintServiceImpl-get入参:{}", JSON.toJSONString(qo));
        GetComplaintDetailVo vo = new GetComplaintDetailVo();
        BaseComplaint baseComplaint = baseComplaintDAO.get(qo.getComplaintId());
        if (CommonUtils.isNotEmpty(baseComplaint)) {
            BeanUtils.copy(baseComplaint, vo);
            //查询反馈内容文件列表
            List<BaseComplaintAttach> fileList = baseComplaintAttachDAO.findComplaintAttach(qo.getComplaintId());
            vo.setFileList(fileList);
            //查询整改列表
            if (qo.getRectIsFlag() == 0) {
                List<BaseComplaintRect> rectList = baseComplaintRectDAO.findComplaintRect(qo.getComplaintId());
                if (CommonUtils.isNotEmpty(rectList)) {
                    ArrayList<GetComplaintDetailBean> list = new ArrayList<>();
                    for (BaseComplaintRect baseComplaintRect : rectList) {
                        GetComplaintDetailBean rectBean = new GetComplaintDetailBean();
                        BeanUtils.copy(baseComplaintRect, rectBean);
                        List<BaseComplaintAttach> rectFileList = baseComplaintAttachDAO.findComplaintAttach(baseComplaintRect.getRectId());
                        rectBean.setFileList(rectFileList);
                        rectBean.setDoctorPhone(getDeptLeaderPhoneNo(rectBean.getDeptId()));
                        list.add(rectBean);
                    }
                    vo.setRectList(list);
                }
            } else {
                //当是医生去处理整改时,只需要展示当前列表中的整改科室
                BaseComplaintRect rect = new BaseComplaintRect();
                if (CommonUtils.isNotEmpty(qo.getRectId())) {
                    rect = baseComplaintRectDAO.get(qo.getRectId());
                }
                if (CommonUtils.isNotEmpty(rect)) {
                    ArrayList<GetComplaintDetailBean> list = new ArrayList<>();
                    GetComplaintDetailBean rectBean = new GetComplaintDetailBean();
                    BeanUtils.copy(rect, rectBean);
                    List<BaseComplaintAttach> rectFileList = baseComplaintAttachDAO.findComplaintAttach(qo.getRectId());
                    rectBean.setFileList(rectFileList);
                    rectBean.setDoctorPhone(getDeptLeaderPhoneNo(rectBean.getDeptId()));
                    list.add(rectBean);
                    vo.setRectList(list);
                }
            }
        }
        return vo;
    }

    private String getDeptLeaderPhoneNo(String deptCode) {
        try {
            DoctorListByMemQo leadQo = new DoctorListByMemQo();
            leadQo.setDeptId(deptCode);
            List<DeptLeaderListRepHisBean.DeptLeaderHisBean> list = patientSearchRuleExecuteService.queryDeptLeaderList(leadQo);
            if(!CollectionUtils.isEmpty(list)){
                return list.get(0).getLXDH();
            }
        }catch (Exception e){
            log.error("获取失败！");
        }
        return null;
    }

    /**
     * @param qo
     * @return com.bsoft.gol.hcs.specialist.bean.vo.FindComplaintListVo
     * @description: 投诉列表查询
     * @author: zsf
     * @date: 2023/10/19 0019 17:07
     */
    @RpcService
    @Override
    public PageInfo<FindComplaintListVo> findComplaintList(FindComplaintListQo qo) throws ControllerException {

        if (qo.getRectQuality() != null && qo.getRectQuality() == 4) {
            qo.setRectFlag((byte) 0);
            qo.setRectQuality(null);
        }
        PageInfo<FindComplaintListVo> vo = new PageInfo<>();
        if (qo.getRectIsFlag() == 0) {
            qo = this.findComplaint(qo);
            //精确到执业点机构列表,和医生所在团队-->医生必须有执业点机构如果没有职业点机构不可以查询数据
            if (CommonUtils.isEmpty(qo.getOrganizationIdList())){
                return null;
            }
            vo = baseComplaintDAO.findComplaintList(qo);
            if (CommonUtils.isNotEmpty(vo)) {
                ArrayList<FindComplaintListVo> bean = new ArrayList<>();
                //有地方要显示整改主责科室
                for (FindComplaintListVo findComplaintListVo : vo.getList()) {
                    String complaintId = findComplaintListVo.getComplaintId();
                    BaseComplaintRect complaintRectMain = baseComplaintRectDAO.getComplaintRectMain(complaintId);
                    if (CommonUtils.isNotEmpty(complaintRectMain)) {
                        findComplaintListVo.setDeptName(complaintRectMain.getDeptName());
                    }
                }
            }
        } else {
            qo = this.findComplaint(qo);
            //精确到执业点机构列表,和医生所在团队-->医生必须有执业点机构如果没有职业点机构不可以查询数据->查询不到团队下的执业机构不可以查询投诉数据
            if (CommonUtils.isEmpty(qo.getOrganizationIdList())){
                return null;
            }
            //获取医生的执业点科室列表
            List<DeptTreeVo> deptDocInfoList = null;
            //管理员没有医生id,老的迁移历史问题
            UserRoleToken token = UserRoleToken.getCurrent();
            //角色代码
            String roleCd = token.getRoleCd();
            //医生id
            String userId = token.getUserId();
            if (CommonUtils.isNotEmpty(userId)) {
                try {
                    deptDocInfoList = hcnDoctorInfoService.findDeptListTreeByUserId(userId);
                    log.info("queryComplaintList查询该医生的执业点科室列表:{}", JSON.toJSONString(deptDocInfoList));
                } catch (ControllerException e) {
                    log.error("queryComplaintList查询医生执业点列表失败");
                }
                ArrayList<String> deptList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(deptDocInfoList)) {
                    for (DeptTreeVo orgDeptDocInfo : deptDocInfoList) {
                        deptList.add(orgDeptDocInfo.getLocalDeptId());
                        if (CommonUtils.isNotEmpty(orgDeptDocInfo.getDepartmentList())) {
                            for (DeptVo deptVo : orgDeptDocInfo.getDepartmentList()) {
                                deptList.add(deptVo.getDepartmentId());
                            }
                        }
                    }
                } else {
                    return null;
                }
                qo.setDeptIdList(deptList);
            }
            //精确到科医生的职业点机构列表,团队,以及科室
            vo = baseComplaintDAO.findComplaintListRect(qo);
        }
        return vo;
    }

    private FindComplaintListQo findComplaint(FindComplaintListQo qo) throws ControllerException {
        //查询执业点机构，所有的都要根据执业点机构过滤，上面几种状态根据科室级别过滤,当是整改列表的时候需要是机构+执业点科室查询的
        if (CommonUtils.isEmpty(qo.getDutyOrganizationId())) {
            QueryAuthorityOrgListQo request = new QueryAuthorityOrgListQo();
            ArrayList<String> modeCode = new ArrayList<>();
            List<QueryAuthorityOrgListVo> organizations = hcnOrganizationRpcService.queryAuthorityOrgList(request);
            log.info("queryComplaintList查询该医生的执业点机构列表:{}", JSON.toJSONString(organizations));
            if (CommonUtils.isNotEmpty(organizations)) {
                List<String> organizationIdList = new ArrayList<>();
                for (QueryAuthorityOrgListVo organization : organizations) {
                    organizationIdList.add(organization.getOrganizationId());
                }
                qo.setOrganizationIdList(organizationIdList);
            }
        } else {
            //这里将机构id置空方便我们查询数据
            List<String> organizationIdList = new ArrayList<>();
            organizationIdList.add(qo.getDutyOrganizationId());
            qo.setOrganizationIdList(organizationIdList);
            qo.setDutyOrganizationId(null);
        }
        FindComplaintTeamListQo request = new FindComplaintTeamListQo();
        request.setDoctorId(qo.getDocId());
        //1投诉处理列表,2投诉审核列表,3整改处理列表,4整改审核列表,5事件评定-->团队控制权限-->团队权限过滤,处理团队中的成员只能处理,
        switch (qo.getQueryType()) {
            case 1:
                request.setOrganizationIdList(qo.getOrganizationIdList());
                //1组长,2辅组长,3成员
                request.setRoleId("3");
                request.setTeamType("6");
                List<String> complaintTeamOrgList = docTeamMemberRpcService.findComplaintTeamList(request);
                qo.setOrganizationIdList(complaintTeamOrgList);
                return qo;
            case 2:
                request.setOrganizationIdList(qo.getOrganizationIdList());
                request.setRoleId("2");
                request.setTeamType("6");
                List<String> complaintTeamOrgLists = docTeamMemberRpcService.findComplaintTeamList(request);
                qo.setOrganizationIdList(complaintTeamOrgLists);
                return qo;
            case 3:
                request.setOrganizationIdList(qo.getOrganizationIdList());
                //整改团队不区分角色
//                request.setRoleId("2");
                request.setTeamType("5");
                List<String> complaintTeamOrgListss = docTeamMemberRpcService.findComplaintTeamList(request);
                qo.setOrganizationIdList(complaintTeamOrgListss);
                return qo;
            case 4:
                request.setOrganizationIdList(qo.getOrganizationIdList());
                request.setRoleId("2");
                request.setTeamType("6");
                List<String> complaintTeamOrgListsss = docTeamMemberRpcService.findComplaintTeamList(request);
                qo.setOrganizationIdList(complaintTeamOrgListsss);
                return qo;
            case 5:
                request.setOrganizationIdList(qo.getOrganizationIdList());
                request.setRoleId("2");
                request.setTeamType("6");
                List<String> complaintTeamOrgListssss = docTeamMemberRpcService.findComplaintTeamList(request);
                qo.setOrganizationIdList(complaintTeamOrgListssss);
                return qo;
            default:
                return qo;
        }
    }

    /**
     * @param qo
     * @return java.util.List<hcn.specialist.BaseComplaintTrace>
     * @description: 查询跟踪记录列表
     * @author: zsf
     * @date: 2023/10/19 0019 13:30
     */
    @RpcService
    @Override
    public List<BaseComplaintTrace> findComplaintTrance(FindComplaintTranceQo qo) {
        log.info("BaseComplaintServiceImpl-findComplaintTrance入参:{}", JSON.toJSONString(qo));
        List<BaseComplaintTrace> list = new ArrayList<>();
        if (CommonUtils.isNotEmpty(qo.getComplaintId())) {
            list = baseComplaintTraceDAO.findComplaintTrace(qo.getComplaintId());
        }
        log.info("BaseComplaintServiceImpl-findComplaintTrance出参:{}", JSON.toJSONString(list));
        return list;
    }

    /**
     * @param qos
     * @return void
     * @description: 新增跟踪记录
     * @author: zsf
     * @date: 2023/10/25 0025 16:28
     */
    @RpcService
    @Override
    public void addComplaintTrance(List<BaseComplaintTrace> qos) {
        if (CommonUtils.isNotEmpty(qos)) {
            for (BaseComplaintTrace qo : qos) {
                qo.setCreateTime(new Date());
                qo.setOperatTime(new Date());
                baseComplaintTraceDAO.save(qo);
            }
        }
    }

    /**
     * @param qo
     * @return void
     * @description: 批量处理列表审核
     * @author: zsf
     * @date: 2023/10/26 0026 11:13
     */
    @RpcService
    @Override
    public void updateCheckComlaintList(List<UpdateCheckComlaintListQo> qo) {
        if (CommonUtils.isNotEmpty(qo)) {
            if (CommonUtils.isNotEmpty(qo)) {
                for (UpdateCheckComlaintListQo complaintBean : qo) {
                    baseComplaintDAO.updateDealQuality(complaintBean.getComplaintId(), complaintBean.getDealQuality(), complaintBean.getStatus());
                }
            }
        }
    }

    /**
     * @param qo
     * @return void
     * @description: 批量整改审核列表
     * @author: zsf
     * @date: 2023/10/26 0026 13:26
     */
    @RpcService
    @Override
    public void updateRectComplaintList(List<UpdateRectComplaintListQo> qo) {
        if (CommonUtils.isNotEmpty(qo)) {
            if (CommonUtils.isNotEmpty(qo)) {
                for (UpdateRectComplaintListQo complaintBean : qo) {
                    baseComplaintDAO.updateRectQuality(complaintBean.getComplaintId(), complaintBean.getRectQuality(), complaintBean.getStatus());
                }
            }
        }
    }

    /**
     * @param qo
     * @return void
     * @description: 评定结果更新
     * @author: zsf
     * @date: 2023/10/26 0026 13:31
     */
    @RpcService
    @Override
    public void updateEvaluateComplaintList(List<UpdateEvaluateComplaintListQo> qo) {
        if (CommonUtils.isNotEmpty(qo)) {
            if (CommonUtils.isNotEmpty(qo)) {
                for (UpdateEvaluateComplaintListQo complaintBean : qo) {
                    baseComplaintDAO.updateEvaluateQuality(complaintBean.getComplaintId(), complaintBean.getEvaluateResult(), complaintBean.getStatus());
                }
            }
        }
    }

    /**
     * 投诉汇总列表
     *
     * @param request
     * @return
     */
    @Override
    @RpcService
    public List<ComplaintStatisticsResponse> complaintStatistics(ComplaintStatisticsRequest request) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime beginTime = LocalDateTime.parse(request.getBeginTime() + " 00:00:00", formatter);
        LocalDateTime endTime = LocalDateTime.parse(request.getEndTime() + " 00:00:00", formatter);
        LocalDateTime firstDayOfMonth = beginTime.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        LocalDateTime lastDayOfMonth = endTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        Date complaintBeginTime = Date.from(firstDayOfMonth.atZone(zoneId).toInstant());
        Date complaintEndTime = Date.from(lastDayOfMonth.atZone(zoneId).toInstant());
        request.setComplaintBeginTime(complaintBeginTime);
        request.setComplaintEndTime(complaintEndTime);

        List<ComplaintStatisticsResponse> vos = new ArrayList<>();
        //月份汇总分析
        if (0 == request.getComplaintObject()) {
            //查询投诉类别1级分类-月份条件
            List<Map> maps = baseComplaintDAO.complaintStatisticsByMonth(request);
            //查询投诉类别2级分类-月份条件
            List<Map> maps1 = baseComplaintDAO.complaintStatisticsMonthSecond(request);
            //查询投诉类别观点
            List<Map> maps2 = baseComplaintDAO.complaintStatisticsMonthPoint(request);
            //查询投诉意见总数
            List<Map> maps3 = baseComplaintDAO.complaintStatisticsMonthOrgPoint(request);
            //查询二级投诉意见总数
            List<Map> maps4 = baseComplaintDAO.complaintStatisticsMonthOrgSecond(request);
            //查询1级投诉意见总数
            List<Map> maps5 = baseComplaintDAO.complaintStatisticsMonthOrgByMonth(request);
            //各个月总的投诉数
            List<Map> maps6 = baseComplaintDAO.complaintStatisticsGroupMonth(request);
            //处理得到投诉第一大类的数据
            Map<String, BigInteger> mapOrgFirst = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps5)) {
                maps5.stream().forEach(l -> {
                    mapOrgFirst.put(String.valueOf(l.get("complaintSortFirstId")), (BigInteger) l.get("total"));
                });
            }
            //医疗质量-2-月份
            BigInteger medicalQualityOrgNum = new BigInteger("0");
            //物价收费-5-月份
            BigInteger thingPayOrgNum = new BigInteger("0");
            //医疗环境-4-月份
            BigInteger medicalEnvirOrgNum = new BigInteger("0");
            //医疗流程-4-月份
            BigInteger medicalProcessOrgNum = new BigInteger("0");
            ;
            if (CommonUtils.isNotEmpty(mapOrgFirst)) {
                medicalQualityOrgNum = mapOrgFirst.get("2") != null ? (BigInteger) mapOrgFirst.get("2") : new BigInteger("0");
                thingPayOrgNum = mapOrgFirst.get("5") != null ? (BigInteger) mapOrgFirst.get("5") : new BigInteger("0");
                medicalEnvirOrgNum = mapOrgFirst.get("4") != null ? (BigInteger) mapOrgFirst.get("4") : new BigInteger("0");
                medicalProcessOrgNum = mapOrgFirst.get("1") != null ? (BigInteger) mapOrgFirst.get("1") : new BigInteger("0");
                //医疗流程
            }
            //处理得到投诉第二大类的数据
            Map<String, BigInteger> mapOrgSecond = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps4)) {
                maps4.stream().forEach(l -> {
                    mapOrgSecond.put(String.valueOf(l.get("complaintSortSecondId")), (BigInteger) l.get("total"));
                });
            }
            //沟通-月份
            BigInteger talkOrgNum = new BigInteger("0");
            ;
            //态度-月份
            BigInteger attitudeOrgNum = new BigInteger("0");
            ;
            //效率-月份
            BigInteger efficyOrgNum = new BigInteger("0");
            ;
            if (CommonUtils.isNotEmpty(mapOrgSecond)) {
                talkOrgNum = mapOrgSecond.get("31") != null ? (BigInteger) mapOrgSecond.get("31") : new BigInteger("0");
                attitudeOrgNum = mapOrgSecond.get("32") != null ? (BigInteger) mapOrgSecond.get("32") : new BigInteger("0");
                efficyOrgNum = mapOrgSecond.get("33") != null ? (BigInteger) mapOrgSecond.get("33") : new BigInteger("0");
            }
            //处理得到投诉满意度数据
            Map<String, BigInteger> mapOrgApoint = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps3)) {
                maps3.stream().forEach(l -> {
                    mapOrgApoint.put(String.valueOf(l.get("complaintantPoint")), (BigInteger) l.get("total"));
                });
            }
            //满意
            BigInteger complaintMoreSatisOrgNum = new BigInteger("0");
            ;
            //欠满意
            BigInteger complaintManySatisOrgNum = new BigInteger("0");
            ;
            //不满意
            BigInteger complaintLessSatisOrgNum = new BigInteger("0");
            ;
            if (CommonUtils.isNotEmpty(mapOrgApoint)) {
                complaintMoreSatisOrgNum = mapOrgApoint.get("1") != null ? (BigInteger) mapOrgApoint.get("1") : new BigInteger("0");
                complaintManySatisOrgNum = mapOrgApoint.get("2") != null ? (BigInteger) mapOrgApoint.get("2") : new BigInteger("0");
                complaintLessSatisOrgNum = mapOrgApoint.get("3") != null ? (BigInteger) mapOrgApoint.get("3") : new BigInteger("0");
            }
            //月份数据处理一级投诉
            Map<String, Map<String, Object>> dataFirst = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps)) {
                maps.stream().forEach(l -> {
                    Map<String, Object> map = dataFirst.get(l.get("month"));
                    if (map == null) {
                        map = new HashMap<>();
                    }
                    map.put(String.valueOf(l.get("complaintSortFirstId")), (BigInteger) l.get("total"));
                    dataFirst.put((String) l.get("month"), map);
                });
            }
            //月份数据处理二级级投诉
            Map<String, Map<String, Object>> dataSecond = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps1)) {
                maps1.stream().forEach(l -> {
                    Map<String, Object> map = dataSecond.get(l.get("month"));
                    if (map == null) {
                        map = new HashMap<>();
                    }
                    map.put(String.valueOf(l.get("complaintSortSecondId")), (BigInteger) l.get("total"));
                    dataSecond.put((String) l.get("month"), map);
                });
            }
            //月份数据处理观点
            Map<String, Map<String, Object>> monthPoint = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps2)) {
                maps2.stream().forEach(l -> {
                    Map<String, Object> map = monthPoint.get(l.get("month"));
                    if (map == null) {
                        map = new HashMap<>();
                    }
                    map.put(String.valueOf(l.get("complaintantPoint")), (BigInteger) l.get("total"));
                    monthPoint.put((String) l.get("month"), map);
                });
            }
            Map<String, BigInteger> groupMonth = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps6)) {
                maps6.stream().forEach(l -> {
                    groupMonth.put((String) l.get("month"), (BigInteger) l.get("total"));
                });
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
            SimpleDateFormat dateFormats = new SimpleDateFormat("MM");
            Calendar begin = Calendar.getInstance();
            try {
                begin.setTime(dateFormat.parse(DateUtils.toStringForYYYYMMdd(request.getComplaintBeginTime())));
                Date end = dateFormat.parse(DateUtils.toStringForYYYYMMdd(request.getComplaintEndTime()));
                while (begin.getTime().before(end) || begin.getTime().equals(end)) {
                    ComplaintStatisticsResponse vo = new ComplaintStatisticsResponse();
                    String yyyyMM = dateFormat.format(begin.getTime());
                    begin.add(Calendar.MONTH, 1);
                    vo.setMonthOrDept(yyyyMM);
                    BigInteger total = groupMonth.get(yyyyMM) != null ? (BigInteger) groupMonth.get(yyyyMM) : new BigInteger("0");
                    vo.setTotal(total != null ? String.valueOf(total) : "0");
                    Map<String, Object> stringFirstMap = dataFirst.get(yyyyMM);
                    if (CommonUtils.isNotEmpty(stringFirstMap)) {
                        //医疗质量-2-月份
                        BigInteger medicalQualityNum = stringFirstMap.get("2") != null ? (BigInteger) stringFirstMap.get("2") : new BigInteger("0");
                        //物价收费-5-月份
                        BigInteger thingPayNum = stringFirstMap.get("5") != null ? (BigInteger) stringFirstMap.get("5") : new BigInteger("0");
                        //医疗环境-4-月份
                        BigInteger medicalEnvirNum = stringFirstMap.get("4") != null ? (BigInteger) stringFirstMap.get("4") : new BigInteger("0");
                        //医疗流程->没有
                        BigInteger medicalProcessNum = stringFirstMap.get("1") != null ? (BigInteger) stringFirstMap.get("1") : new BigInteger("0");
                        vo.setMedicaQualitylNum(medicalQualityNum != null ? String.valueOf(medicalQualityNum) : "0");
                        vo.setPriceChargeNum(thingPayNum != null ? String.valueOf(thingPayNum) : "0");
                        vo.setMedicalEnvironNum(medicalEnvirNum != null ? String.valueOf(medicalEnvirNum) : "0");
                        vo.setMedicalProcessNum(medicalProcessNum != null ? String.valueOf(medicalProcessNum) : "0");
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = medicalQualityNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setMedicaQualitylPercent(formattedPercentage);
                        } else {
                            vo.setMedicaQualitylPercent("0");
                        }
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = thingPayNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setPriceChargePercent(formattedPercentage);
                        } else {
                            vo.setPriceChargePercent("0");
                        }
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = medicalEnvirNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setMedicalEnvironPercent(formattedPercentage);
                        } else {
                            vo.setMedicalEnvironPercent("0");
                        }
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = medicalProcessNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setMedicalProcessPercent(formattedPercentage);
                        } else {
                            vo.setMedicalProcessPercent("0");
                        }
                    } else {
                        vo.setMedicaQualitylNum("0");
                        vo.setMedicaQualitylPercent("0");
                        vo.setPriceChargeNum("0");
                        vo.setPriceChargePercent("0");
                        vo.setMedicalEnvironNum("0");
                        vo.setMedicalEnvironPercent("0");
                        vo.setMedicalProcessNum("0");
                        vo.setMedicalProcessPercent("0");
                    }
                    Map<String, Object> stringSecondMap = dataSecond.get(yyyyMM);
                    if (CommonUtils.isNotEmpty(stringSecondMap)) {
                        //沟通-月份
                        BigInteger talkNum = stringSecondMap.get("31") != null ? (BigInteger) stringSecondMap.get("31") : new BigInteger("0");
                        //态度-月份
                        BigInteger attitudeNum = stringSecondMap.get("32") != null ? (BigInteger) stringSecondMap.get("32") : new BigInteger("0");
                        //效率-月份
                        BigInteger efficyNum = stringSecondMap.get("33") != null ? (BigInteger) stringSecondMap.get("33") : new BigInteger("0");
                        vo.setTalkNum(talkNum != null ? String.valueOf(talkNum) : "0");
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = talkNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setTalkPercent(formattedPercentage);
                        } else {
                            vo.setTalkPercent("0");
                        }
                        vo.setAttitudeNum(attitudeNum != null ? String.valueOf(attitudeNum) : "0");
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = attitudeNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setAttitudePercent(formattedPercentage);
                        } else {
                            vo.setAttitudePercent("0");
                        }
                        vo.setEfficencyNum(efficyNum != null ? String.valueOf(efficyNum) : "0");
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = efficyNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setEfficencyPercent(formattedPercentage);
                        } else {
                            vo.setEfficencyPercent("0");
                        }
                    } else {
                        vo.setTalkNum("0");
                        vo.setTalkPercent("0");
                        vo.setAttitudeNum("0");
                        vo.setAttitudePercent("0");
                        vo.setEfficencyNum("0");
                        vo.setEfficencyPercent("0");
                    }
                    Map<String, Object> stringObjectMap = monthPoint.get(yyyyMM);
                    if (CommonUtils.isNotEmpty(stringObjectMap)) {
                        //满意
                        BigInteger complaintMoreSatisNum = stringObjectMap.get("1") != null ? (BigInteger) stringObjectMap.get("1") : new BigInteger("0");
                        //欠满意
                        BigInteger complaintManySatisNum = stringObjectMap.get("2") != null ? (BigInteger) stringObjectMap.get("2") : new BigInteger("0");
                        //很满意
                        BigInteger complaintLessSatisNum = stringObjectMap.get("3") != null ? (BigInteger) stringObjectMap.get("3") : new BigInteger("0");
                        vo.setComplaintMoreSatisNum(complaintMoreSatisNum != null ? String.valueOf(complaintMoreSatisNum) : "0");
                        vo.setComplaintManySatisNum(complaintManySatisNum != null ? String.valueOf(complaintManySatisNum) : "0");
                        vo.setComplaintLessSatisNum(complaintLessSatisNum != null ? String.valueOf(complaintLessSatisNum) : "0");
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = complaintMoreSatisNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setComplaintMoreSatisPercent(formattedPercentage);
                        } else {
                            vo.setComplaintMoreSatisPercent("0");
                        }
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = complaintManySatisNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setComplaintManySatisPercent(formattedPercentage);
                        } else {
                            vo.setComplaintManySatisPercent("0");
                        }
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = complaintLessSatisNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setComplaintLessSatisPercent(formattedPercentage);
                        } else {
                            vo.setComplaintLessSatisPercent("0");
                        }
                    } else {
                        vo.setComplaintMoreSatisNum("0");
                        vo.setComplaintManySatisNum("0");
                        vo.setComplaintLessSatisNum("0");
                        vo.setComplaintManySatisPercent("0");
                        vo.setComplaintMoreSatisPercent("0");
                        vo.setComplaintLessSatisPercent("0");
                    }
                    vos.add(vo);
                }
                ComplaintStatisticsResponse bean = new ComplaintStatisticsResponse();
                bean.setMonthOrDept("合计");
                List<Map> map6 = baseComplaintDAO.complaintStatisticsOrgCount(request);
                BigInteger orgTotal = map6.get(0) != null ? (BigInteger) map6.get(0).get("total") : new BigInteger("0");
                bean.setTotal(String.valueOf(orgTotal));
                bean.setMedicaQualitylNum(String.valueOf(medicalQualityOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = medicalQualityOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setMedicaQualitylPercent(formattedPercentage);
                } else {
                    bean.setMedicaQualitylPercent("0");
                }
                bean.setPriceChargeNum(String.valueOf(thingPayOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = thingPayOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setPriceChargePercent(formattedPercentage);
                } else {
                    bean.setPriceChargePercent("0");
                }
                bean.setMedicalEnvironNum(String.valueOf(medicalEnvirOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = medicalEnvirOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setMedicalEnvironPercent(formattedPercentage);
                } else {
                    bean.setMedicalEnvironPercent("0");
                }
                bean.setTalkNum(String.valueOf(talkOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = talkOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setTalkPercent(formattedPercentage);
                } else {
                    bean.setTalkPercent("0");
                }
                bean.setAttitudeNum(String.valueOf(attitudeOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = attitudeOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setAttitudePercent(formattedPercentage);
                } else {
                    bean.setAttitudePercent("0");
                }
                bean.setEfficencyNum(String.valueOf(efficyOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = efficyOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setEfficencyPercent(formattedPercentage);
                } else {
                    bean.setEfficencyPercent("0");
                }
                bean.setMedicalProcessNum(String.valueOf(medicalProcessOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = medicalProcessOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setMedicalProcessPercent(formattedPercentage);
                } else {
                    bean.setMedicalProcessPercent("0");
                }
                bean.setComplaintMoreSatisNum(String.valueOf(complaintMoreSatisOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = complaintMoreSatisOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setComplaintMoreSatisPercent(formattedPercentage);
                } else {
                    bean.setComplaintMoreSatisPercent("0");
                }
                bean.setComplaintManySatisNum(String.valueOf(complaintManySatisOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = complaintManySatisOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setComplaintManySatisPercent(formattedPercentage);
                } else {
                    bean.setComplaintManySatisPercent("0");
                }
                bean.setComplaintLessSatisNum(String.valueOf(complaintLessSatisOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = complaintLessSatisOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setComplaintLessSatisPercent(formattedPercentage);
                } else {
                    bean.setComplaintLessSatisPercent("0");
                }
                vos.add(bean);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        } else {
            //科室汇总分析
            List<Map> maps = baseComplaintDAO.complaintStatisticsByDept(request);
            List<Map> maps1 = baseComplaintDAO.complaintStatisticsDeptSecond(request);
            List<Map> maps2 = baseComplaintDAO.complaintStatisticsDeptPoint(request);
            //机构投诉人总数
            List<Map> maps3 = baseComplaintDAO.complaintStatisticsMonthOrgPoint(request);
            List<Map> maps4 = baseComplaintDAO.complaintStatisticsMonthOrgSecond(request);
            List<Map> maps5 = baseComplaintDAO.complaintStatisticsMonthOrgByMonth(request);
            List<Map> maps6 = baseComplaintDAO.complaintStatisticsGroupDept(request);
            //处理得到投诉第一大类的数据
            Map<String, BigInteger> mapOrgFirst = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps5)) {
                maps5.stream().forEach(l -> {
                    mapOrgFirst.put(String.valueOf(l.get("complaintSortFirstId")), (BigInteger) l.get("total"));
                });
            }
            //医疗质量-2-月份
            BigInteger medicalQualityOrgNum = new BigInteger("0");
            //物价收费-5-月份
            BigInteger thingPayOrgNum = new BigInteger("0");
            //医疗环境-4-月份
            BigInteger medicalEnvirOrgNum = new BigInteger("0");
            //医疗流程-4-月份
            BigInteger medicalProcessOrgNum = new BigInteger("0");
            if (CommonUtils.isNotEmpty(mapOrgFirst)) {
                medicalQualityOrgNum = (BigInteger) mapOrgFirst.get("2") != null ? (BigInteger) mapOrgFirst.get("2") : new BigInteger("0");
                thingPayOrgNum = (BigInteger) mapOrgFirst.get("5") != null ? (BigInteger) mapOrgFirst.get("5") : new BigInteger("0");
                medicalEnvirOrgNum = (BigInteger) mapOrgFirst.get("4") != null ? (BigInteger) mapOrgFirst.get("4") : new BigInteger("0");
                medicalProcessOrgNum = (BigInteger) mapOrgFirst.get("1") != null ? (BigInteger) mapOrgFirst.get("1") : new BigInteger("0");
                //医疗流程
            }
            //处理得到投诉第二大类的数据
            Map<String, BigInteger> mapOrgSecond = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps4)) {
                maps4.stream().forEach(l -> {
                    mapOrgSecond.put(String.valueOf(l.get("complaintSortSecondId")), (BigInteger) l.get("total"));
                });
            }
            //沟通-月份
            BigInteger talkOrgNum = new BigInteger("0");
            ;
            //态度-月份
            BigInteger attitudeOrgNum = new BigInteger("0");
            ;
            //效率-月份
            BigInteger efficyOrgNum = new BigInteger("0");
            ;
            if (CommonUtils.isNotEmpty(mapOrgSecond)) {
                talkOrgNum = mapOrgSecond.get("31") != null ? (BigInteger) mapOrgSecond.get("31") : new BigInteger("0");
                attitudeOrgNum = mapOrgSecond.get("32") != null ? (BigInteger) mapOrgSecond.get("32") : new BigInteger("0");
                efficyOrgNum = mapOrgSecond.get("33") != null ? (BigInteger) mapOrgSecond.get("33") : new BigInteger("0");
            }
            //处理得到投诉满意度数据
            Map<String, BigInteger> mapOrgApoint = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps3)) {
                maps3.stream().forEach(l -> {
                    mapOrgApoint.put(String.valueOf(l.get("complaintantPoint")), (BigInteger) l.get("total"));
                });
            }
            //满意
            BigInteger complaintMoreSatisOrgNum = new BigInteger("0");
            ;
            //欠满意
            BigInteger complaintManySatisOrgNum = new BigInteger("0");
            ;
            //不满意
            BigInteger complaintLessSatisOrgNum = new BigInteger("0");
            ;
            if (CommonUtils.isNotEmpty(mapOrgApoint)) {
                complaintMoreSatisOrgNum = mapOrgApoint.get("1") != null ? (BigInteger) mapOrgApoint.get("1") : new BigInteger("0");
                complaintManySatisOrgNum = mapOrgApoint.get("2") != null ? (BigInteger) mapOrgApoint.get("2") : new BigInteger("0");
                complaintLessSatisOrgNum = mapOrgApoint.get("3") != null ? (BigInteger) mapOrgApoint.get("3") : new BigInteger("0");
            }
            //月份数据处理一级投诉
            Map<String, Map<String, Object>> dataFirst = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps)) {
                maps.stream().forEach(l -> {
                    Map<String, Object> map = dataFirst.get((String) l.get("deptIdFirst") + (String) l.get("complaintDeptId"));
                    if (map == null) {
                        map = new HashMap<>();
                    }
                    map.put(String.valueOf(l.get("complaintSortFirstId")), (BigInteger) l.get("total"));
                    dataFirst.put((String) l.get("deptIdFirst") + (String) l.get("complaintDeptId"), map);
                });
            }
            //月份数据处理二级级投诉
            Map<String, Map<String, Object>> dataSecond = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps1)) {
                maps1.stream().forEach(l -> {
                    Map<String, Object> map = dataSecond.get((String) l.get("deptIdFirst") + (String) l.get("complaintDeptId"));
                    if (map == null) {
                        map = new HashMap<>();
                    }
                    map.put(String.valueOf(l.get("complaintSortSecondId")), (BigInteger) l.get("total"));
                    dataSecond.put((String) l.get("deptIdFirst") + (String) l.get("complaintDeptId"), map);
                });
            }
            //月份数据处理观点
            Map<String, Map<String, Object>> monthPoint = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps2)) {
                maps2.stream().forEach(l -> {
                    Map<String, Object> map = monthPoint.get((String) l.get("deptIdFirst") + (String) l.get("complaintDeptId"));
                    if (map == null) {
                        map = new HashMap<>();
                    }
                    map.put(String.valueOf(l.get("complaintantPoint")), (BigInteger) l.get("total"));
                    monthPoint.put((String) l.get("deptIdFirst") + (String) l.get("complaintDeptId"), map);
                });
            }
            //各个科室的投诉总数
            Map<String, BigInteger> groupDept = new HashMap<>();
            if (CommonUtils.isNotEmpty(maps6)) {
                maps6.stream().forEach(l -> {
                    groupDept.put((String) l.get("deptIdFirst") + (String) l.get("complaintDeptId"), (BigInteger) l.get("total"));
                });
            }
            try {
                for (Map map : maps6) {
                    String complaintDeptId = (String) map.get("complaintDeptId");
                    String complaintDeptName = (String) map.get("complaintDeptName");
                    String deptIdFirst = (String) map.get("deptIdFirst");
                    String deptNameFirst = (String) map.get("deptNameFirst");
                    String key = deptIdFirst + complaintDeptId;
                    ComplaintStatisticsResponse vo = new ComplaintStatisticsResponse();
                    if (CommonUtils.isEmpty(complaintDeptName) && CommonUtils.isEmpty(deptNameFirst)) {
                        vo.setMonthOrDept("医院");
                    } else if (CommonUtils.isNotEmpty(complaintDeptName)) {
                        vo.setMonthOrDept(complaintDeptName);
                    } else if (CommonUtils.isNotEmpty(deptNameFirst)) {
                        vo.setMonthOrDept(deptNameFirst);
                    }
                    BigInteger total = groupDept.get(key) != null ? (BigInteger) groupDept.get(key) : new BigInteger("0");
                    vo.setTotal(total != null ? String.valueOf(total) : "0");
                    Map<String, Object> stringFirstMap = dataFirst.get(key);
                    if (CommonUtils.isNotEmpty(stringFirstMap)) {
                        //医疗质量-2-月份
                        BigInteger medicalQualityNum = stringFirstMap.get("2") != null ? (BigInteger) stringFirstMap.get("2") : new BigInteger("0");
                        //医疗流程-1>没有
                        BigInteger medicalProcessNum = stringFirstMap.get("1") != null ? (BigInteger) stringFirstMap.get("1") : new BigInteger("0");
                        //物价收费-5-月份
                        BigInteger thingPayNum = stringFirstMap.get("5") != null ? (BigInteger) stringFirstMap.get("5") : new BigInteger("0");
                        //医疗环境-4-月份
                        BigInteger medicalEnvirNum = stringFirstMap.get("4") != null ? (BigInteger) stringFirstMap.get("4") : new BigInteger("0");
                        vo.setMedicaQualitylNum(medicalQualityNum != null ? String.valueOf(medicalQualityNum) : "0");
                        vo.setPriceChargeNum(thingPayNum != null ? String.valueOf(thingPayNum) : "0");
                        vo.setMedicalEnvironNum(medicalEnvirNum != null ? String.valueOf(medicalEnvirNum) : "0");
                        vo.setMedicalProcessNum(medicalProcessNum != null ? String.valueOf(medicalProcessNum) : "0");
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = medicalQualityNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setMedicaQualitylPercent(formattedPercentage);
                        } else {
                            vo.setMedicaQualitylPercent("0");
                        }
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = thingPayNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setPriceChargePercent(formattedPercentage);
                        } else {
                            vo.setPriceChargePercent("0");
                        }
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = medicalEnvirNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setMedicalEnvironPercent(formattedPercentage);
                        } else {
                            vo.setMedicalEnvironPercent("0");
                        }
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = medicalProcessNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setMedicalProcessPercent(formattedPercentage);
                        } else {
                            vo.setMedicalProcessPercent("0");
                        }
                    } else {
                        vo.setMedicaQualitylNum("0");
                        vo.setMedicaQualitylPercent("0");
                        vo.setPriceChargeNum("0");
                        vo.setPriceChargePercent("0");
                        vo.setMedicalEnvironNum("0");
                        vo.setMedicalEnvironPercent("0");
                        vo.setMedicalProcessNum("0");
                        vo.setMedicalProcessPercent("0");
                    }
                    Map<String, Object> stringSecondMap = dataSecond.get(key);
                    if (CommonUtils.isNotEmpty(stringSecondMap)) {
                        //沟通-月份
                        BigInteger talkNum = stringSecondMap.get("31") != null ? (BigInteger) stringSecondMap.get("31") : new BigInteger("0");
                        //态度-月份
                        BigInteger attitudeNum = stringSecondMap.get("32") != null ? (BigInteger) stringSecondMap.get("32") : new BigInteger("0");
                        //效率-月份
                        BigInteger efficyNum = stringSecondMap.get("33") != null ? (BigInteger) stringSecondMap.get("33") : new BigInteger("0");
                        vo.setTalkNum(talkNum != null ? String.valueOf(talkNum) : "0");
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = talkNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setTalkPercent(formattedPercentage);
                        } else {
                            vo.setTalkPercent("0");
                        }
                        vo.setAttitudeNum(total != null ? String.valueOf(attitudeNum) : "0");
                        if (!attitudeOrgNum.equals(new BigInteger("0"))) {
                            double value1 = attitudeNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setAttitudePercent(formattedPercentage);
                        } else {
                            vo.setAttitudePercent("0");
                        }
                        vo.setEfficencyNum(efficyNum != null ? String.valueOf(efficyNum) : "0");
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = efficyNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setEfficencyPercent(formattedPercentage);
                        } else {
                            vo.setEfficencyPercent("0");
                        }
                    } else {
                        vo.setTalkNum("0");
                        vo.setTalkPercent("0");
                        vo.setAttitudeNum("0");
                        vo.setAttitudePercent("0");
                        vo.setEfficencyNum("0");
                        vo.setEfficencyPercent("0");
                    }
                    Map<String, Object> stringObjectMap = monthPoint.get(key);
                    if (CommonUtils.isNotEmpty(stringObjectMap)) {
                        //满意
                        BigInteger complaintMoreSatisNum = stringObjectMap.get("1") != null ? (BigInteger) stringObjectMap.get("1") : new BigInteger("0");
                        //欠满意
                        BigInteger complaintManySatisNum = stringObjectMap.get("2") != null ? (BigInteger) stringObjectMap.get("2") : new BigInteger("0");
                        //很满意
                        BigInteger complaintLessSatisNum = stringObjectMap.get("3") != null ? (BigInteger) stringObjectMap.get("3") : new BigInteger("0");
                        vo.setComplaintMoreSatisNum(complaintMoreSatisNum != null ? String.valueOf(complaintMoreSatisNum) : "0");
                        vo.setComplaintManySatisNum(complaintManySatisNum != null ? String.valueOf(complaintManySatisNum) : "0");
                        vo.setComplaintLessSatisNum(complaintLessSatisNum != null ? String.valueOf(complaintLessSatisNum) : "0");
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = complaintMoreSatisNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setComplaintMoreSatisPercent(formattedPercentage);
                        } else {
                            vo.setComplaintMoreSatisPercent("0");
                        }
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = complaintManySatisNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setComplaintManySatisPercent(formattedPercentage);
                        } else {
                            vo.setComplaintManySatisPercent("0");
                        }
                        if (!total.equals(new BigInteger("0"))) {
                            double value1 = complaintLessSatisNum.doubleValue();
                            double value2 = total.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setComplaintLessSatisPercent(formattedPercentage);
                        } else {
                            vo.setComplaintLessSatisPercent("0");
                        }
                    } else {
                        vo.setComplaintMoreSatisNum("0");
                        vo.setComplaintManySatisNum("0");
                        vo.setComplaintLessSatisNum("0");
                        vo.setComplaintManySatisPercent("0");
                        vo.setComplaintMoreSatisPercent("0");
                        vo.setComplaintLessSatisPercent("0");
                    }
                    vos.add(vo);
                }
                ComplaintStatisticsResponse bean = new ComplaintStatisticsResponse();
                bean.setMonthOrDept("合计");
                List<Map> map6 = baseComplaintDAO.complaintStatisticsOrgCount(request);
                BigInteger orgTotal = map6.get(0) != null ? (BigInteger) map6.get(0).get("total") : new BigInteger("0");
                bean.setTotal(String.valueOf(orgTotal));
                bean.setMedicaQualitylNum(String.valueOf(medicalQualityOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = medicalQualityOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setMedicaQualitylPercent(formattedPercentage);
                } else {
                    bean.setMedicaQualitylPercent("0");
                }
                bean.setPriceChargeNum(String.valueOf(thingPayOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = thingPayOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setPriceChargePercent(formattedPercentage);
                } else {
                    bean.setPriceChargePercent("0");
                }
                bean.setMedicalEnvironNum(String.valueOf(medicalEnvirOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = medicalEnvirOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setMedicalEnvironPercent(formattedPercentage);
                } else {
                    bean.setMedicalEnvironPercent("0");
                }
                bean.setTalkNum(String.valueOf(talkOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = talkOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setTalkPercent(formattedPercentage);
                } else {
                    bean.setTalkPercent("0");
                }
                bean.setAttitudeNum(String.valueOf(attitudeOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = attitudeOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setAttitudePercent(formattedPercentage);
                } else {
                    bean.setAttitudePercent("0");
                }
                bean.setEfficencyNum(String.valueOf(efficyOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = efficyOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setEfficencyPercent(formattedPercentage);
                } else {
                    bean.setEfficencyPercent("0");
                }
                bean.setMedicalProcessNum(String.valueOf(medicalProcessOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = medicalProcessOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setMedicalProcessPercent(formattedPercentage);
                } else {
                    bean.setMedicalProcessPercent("0");
                }
                bean.setComplaintMoreSatisNum(String.valueOf(complaintMoreSatisOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = complaintMoreSatisOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setComplaintMoreSatisPercent(formattedPercentage);
                } else {
                    bean.setComplaintMoreSatisPercent("0");
                }
                bean.setComplaintManySatisNum(String.valueOf(complaintManySatisOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = complaintManySatisOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setComplaintManySatisPercent(formattedPercentage);
                } else {
                    bean.setComplaintManySatisPercent("0");
                }
                bean.setComplaintLessSatisNum(String.valueOf(complaintLessSatisOrgNum));
                if (!orgTotal.equals(new BigInteger("0"))) {
                    double value1 = complaintLessSatisOrgNum.doubleValue();
                    double value2 = orgTotal.doubleValue();
                    // 计算百分比
                    double percentage = (value1 / value2) * 100;
                    // 使用 DecimalFormat 格式化百分比
                    DecimalFormat df = new DecimalFormat("0.0");
                    String formattedPercentage = df.format(percentage);
                    bean.setComplaintLessSatisPercent(formattedPercentage);
                } else {
                    bean.setComplaintLessSatisPercent("0");
                }
                vos.add(bean);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return vos;
    }


    /**
     * 投诉汇总列表
     *
     * @param request
     * @return
     */
    @SneakyThrows
    @Override
    @RpcService
    public List<ComplaintDeptStatisticsResponse> complaintDeptStatistics(ComplaintStatisticsRequest request) {

        List<ComplaintDeptStatisticsResponse> list = new ArrayList<>();
        //获取月份
        List<Map> complaintDeptIdList = baseComplaintDAO.findComplaintDeptIdList(request);
        Map<String, HashMap<String, Object>> stringMapMap = baseComplaintDAO.complaintDeptStatistics(request);
        //全院的投诉比
        long totalCount = baseComplaintDAO.getComplaintDeptIdList(request.getComplaintYear());
        if (CommonUtils.isNotEmpty(complaintDeptIdList)) {
            for (Map map : complaintDeptIdList) {
                ComplaintDeptStatisticsResponse vo = new ComplaintDeptStatisticsResponse();
                String deptId = (String) map.get("complaintDeptId");
                String complaintDeptName = (String) map.get("complaintDeptName");
                //一级科室
                String deptIdFirst = (String) map.get("deptIdFirst");
                String deptNameFirst = (String) map.get("deptNameFirst");
                if (CommonUtils.isEmpty(complaintDeptName) && CommonUtils.isEmpty(deptNameFirst)) {
                    vo.setDeptName("医院");
                } else if (CommonUtils.isNotEmpty(complaintDeptName)) {
                    vo.setDeptName(complaintDeptName);
                } else if (CommonUtils.isNotEmpty(deptNameFirst)) {
                    vo.setDeptName(deptNameFirst);
                }
                Map<String, Object> stringObjectMap = stringMapMap.get(deptIdFirst + deptId);
                // 创建一个Calendar对象，并设置为输入年份的一月一日
                Calendar calendar = Calendar.getInstance();
                calendar.set(Integer.valueOf(request.getComplaintYear()), Calendar.JANUARY, 1);
                // 设置时间格式为yyyy-MM
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
                // 循环遍历一年中的每个月，并打印年份和月份
                BigInteger bigIntegerAllYear = new BigInteger("0");
                BigInteger bigIntegerHalfYear = new BigInteger("0");
                for (int i = 1; i <= 12; i++) {
                    String format = dateFormat.format(calendar.getTime());
                    //拿到低一个月的投诉数量
                    BigInteger bigInteger = stringObjectMap != null ? (BigInteger) stringObjectMap.get(format) : new BigInteger("0");
                    // 月份加1
                    calendar.add(Calendar.MONTH, 1);
                    if (CommonUtils.isEmpty(bigInteger)) {
                        bigInteger = new BigInteger("0");
                    }
                    if (i <= 6) {
                        //算出半年的总数
                        bigIntegerHalfYear = bigIntegerHalfYear.add(bigInteger);
                    }
                    if (i == 6) {
                        vo.setHalfYear(String.valueOf(bigIntegerHalfYear));
                    }
                    bigIntegerAllYear = bigIntegerAllYear.add(bigInteger);
                    if (i == 12) {
                        vo.setAllYear(String.valueOf(bigIntegerAllYear));
                        BigInteger totalNum = BigInteger.valueOf(totalCount);
                        if (totalCount != new Long(0)) {
                            // 将 BigInteger 对象转换为 double 值
                            double value1 = bigIntegerAllYear.doubleValue();
                            double value2 = totalNum.doubleValue();
                            // 计算百分比
                            double percentage = (value1 / value2) * 100;
                            // 使用 DecimalFormat 格式化百分比
                            DecimalFormat df = new DecimalFormat("0.0");
                            String formattedPercentage = df.format(percentage);
                            vo.setHospitalPercent(String.valueOf(formattedPercentage));
                        }
                        BigInteger subtract = bigIntegerAllYear.subtract(bigIntegerHalfYear);
                        vo.setLastHalfYear(String.valueOf(subtract));
                    }
                    switch (i) {
                        case 1:
                            vo.setJanuary(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        case 2:
                            vo.setFebruary(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        case 3:
                            vo.setMarch(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        case 4:
                            vo.setApril(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        case 5:
                            vo.setMay(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        case 6:
                            vo.setJune(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        case 7:
                            vo.setJuly(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        case 8:
                            vo.setAugust(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        case 9:
                            vo.setSeptember(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        case 10:
                            vo.setOctober(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        case 11:
                            vo.setNovember(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        case 12:
                            vo.setDecember(CommonUtils.isNotEmpty(bigInteger) ? String.valueOf(bigInteger) : "0");
                            continue;
                        default:
                            break;
                    }
                }
                list.add(vo);
            }
        }
        return list;
    }
}
